/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.EnregistrementAudio;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public class FormEnregistrementAudio extends FormAbstractFichier<EnregistrementAudio> {

    private String numAppelant;

    /**
     *
     * @param entity
     */
    public FormEnregistrementAudio(EnregistrementAudio entity) {
        super(new FormEnregistrementAudioId(entity.getId()), entity);
        this.numAppelant = entity.getNumeroAppellant();
    }

    /**
     *
     * @param request
     */
    public FormEnregistrementAudio(HttpServletRequest request) {
        super(new FormEnregistrementAudioId(request), request);
        this.numAppelant = this.getNonNullStringFromRequest(request, "numAppelant");
    }

    @Override
    protected void validate(boolean creation) throws SpotExceptionForm {
        boolean erreur = false;
        try {
            super.validate(creation);
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        if (creation) {
            try {
                this.isPhoneNumber(numAppelant, "numéro appelant");
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        }
        if (erreur) {
            throw new SpotExceptionForm(msgErreurValidate);
        }
    }

    @Override
    public EnregistrementAudio getEntityToPersist() throws SpotExceptionForm {
        return this.garnir(new EnregistrementAudio(), true);
    }

    /**
     *
     * @param entityToFeed
     * @return
     * @throws SpotExceptionForm
     */
    @Override
    protected EnregistrementAudio garnir(EnregistrementAudio entityToFeed, boolean creation) throws SpotExceptionForm {
        entityToFeed = super.garnir(entityToFeed, creation);
        if (creation) {
            entityToFeed.setNumeroAppellant(numAppelant);
        }
        return entityToFeed;
    }

    /**
     *
     * @return
     */
    public String getNumAppelant() {
        return numAppelant;
    }

    /**
     *
     * @param numAppelant
     */
    public void setNumAppelant(String numAppelant) {
        this.numAppelant = numAppelant;
    }
}